package com.eastfair.imaster.controller;

import com.eastfair.imaster.entity.CustomerPool;
import com.eastfair.imaster.dto.CustomerPoolSaveDTO;
import com.eastfair.imaster.dto.CustomerPoolUpdateDTO;
import com.eastfair.imaster.dto.CustomerPoolPageQuery;
import com.eastfair.imaster.service.CustomerPoolService;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.eastfair.boot.controller.SuperController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.echo.core.EchoService;
import com.eastfair.core.base.R;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.eastfair.boot.request.PageParams;
import com.eastfair.core.context.ContextUtil;
import org.springframework.web.bind.annotation.RestController;


/**
 * <p>
 * 前端控制器
 * 客户池
 * </p>
 *
 * @author liuxiaobai
 * @date 2021-11-15
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/customerPool")
@Api(value = "CustomerPool", tags = "客户池")
public class CustomerPoolController extends SuperController<CustomerPoolService, Long, CustomerPool, CustomerPoolPageQuery, CustomerPoolSaveDTO, CustomerPoolUpdateDTO> {

    @Autowired
    private EchoService echoService;

    @Override
    public void handlerResult(IPage<CustomerPool> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<CustomerPool> customerPoolList = list.stream().map((map) -> {
            CustomerPool customerPool = CustomerPool.builder().build();
            //TODO 请在这里完成转换
            return customerPool;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(customerPoolList));
    }

    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<CustomerPoolPageQuery> params) {
        CustomerPoolPageQuery pageQuery = params.getModel();
        //分页处理
        if(ContextUtil.getProjectId()!=null){
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }

    @PostMapping("/addPool")
    public R<Boolean> addPool(CustomerPoolSaveDTO saveDTO) {
        return success();
    }
}
